﻿package mortal.component.rotate3dModelSprite
{
    import baseEngine.core.*;
    import com.gengine.core.frame.*;
    import com.gengine.global.*;
    import com.mui.controls.*;
    import flash.events.*;
    import mortal.common.global.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.mvc.core.*;

    public class Rotate3dModelSprite extends GSprite
    {
        private var _model:Pivot3D;
        private var _startX:int;
        private var _endX:int;
        private var _stageMouseDown:Boolean;
        private var _frameTimer:FrameTimer;
        private var _turnValue:int;
        private var _isAddListeners:Boolean;

        public function Rotate3dModelSprite()
        {
            return;
        }// end function

        private function onStageMouseDown(event:MouseEvent) : void
        {
            this._stageMouseDown = true;
            return;
        }// end function

        private function onStageMouseUp(event:MouseEvent) : void
        {
            this._stageMouseDown = false;
            return;
        }// end function

        private function onRollOver(event:MouseEvent) : void
        {
            if (this._stageMouseDown)
            {
                this.onMouseDown();
            }
            return;
        }// end function

        private function onMouseDown(event:MouseEvent = null) : void
        {
            this._startX = mouseX;
            this.configEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMove);
            this.configEventListener(MouseEvent.ROLL_OUT, this.onMouseUp);
            return;
        }// end function

        private function onMouseUp(event:MouseEvent) : void
        {
            this.removeEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMove);
            this.removeEventListener(MouseEvent.ROLL_OUT, this.onMouseUp);
            return;
        }// end function

        private function onMouseMove(event:MouseEvent) : void
        {
            if (this._model)
            {
                this._endX = this.mouseX;
                this._model.rotationY = this._model.rotationY - (this._endX - this._startX) / 3;
                this._startX = this._endX;
            }
            return;
        }// end function

        public function set model(param1:Pivot3D) : void
        {
            this._model = param1;
            return;
        }// end function

        override public function setSize(param1:Number, param2:Number) : void
        {
            super.setSize(param1, param2);
            this.graphics.clear();
            this.graphics.beginFill(GlobalStyle.colorPutongUint, 0);
            this.graphics.drawRect(0, 0, param1, param2);
            this.graphics.endFill();
            return;
        }// end function

        private function onModelTurnLeftHandler(event:DataEvent) : void
        {
            this._turnValue = 2;
            this.start();
            return;
        }// end function

        private function onModelTurnRightHandler(event:DataEvent) : void
        {
            this._turnValue = -2;
            this.start();
            return;
        }// end function

        private function onModelStopTurningHandler(event:DataEvent) : void
        {
            this.stopTurning();
            return;
        }// end function

        private function start() : void
        {
            if (!this._frameTimer)
            {
                this._frameTimer = new FrameTimer(1, int.MAX_VALUE, true);
                this._frameTimer.addListener(TimerType.ENTERFRAME, this.onTurning);
            }
            this._frameTimer.start();
            return;
        }// end function

        private function onTurning(param1:FrameTimer) : void
        {
            if (this._model)
            {
                this._model.rotationY = this._model.rotationY + this._turnValue;
            }
            return;
        }// end function

        private function stopTurning() : void
        {
            if (this._frameTimer)
            {
                this._frameTimer.stop();
            }
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.mouseEnabled = true;
            this.configEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDown);
            this.configEventListener(MouseEvent.MOUSE_UP, this.onMouseUp);
            this.configEventListener(MouseEvent.ROLL_OVER, this.onRollOver);
            Global.stage.addEventListener(MouseEvent.MOUSE_DOWN, this.onStageMouseDown);
            Global.stage.addEventListener(MouseEvent.MOUSE_UP, this.onStageMouseUp);
            return;
        }// end function

        public function addListeners() : void
        {
            this._isAddListeners = true;
            Dispatcher.addEventListener(EventName.FashionModelTurnLeft, this.onModelTurnLeftHandler);
            Dispatcher.addEventListener(EventName.FashionModelTurnRight, this.onModelTurnRightHandler);
            Dispatcher.addEventListener(EventName.FashionModelStopTurning, this.onModelStopTurningHandler);
            return;
        }// end function

        private function removeListeners() : void
        {
            this._isAddListeners = false;
            Dispatcher.removeEventListener(EventName.FashionModelTurnLeft, this.onModelTurnLeftHandler);
            Dispatcher.removeEventListener(EventName.FashionModelTurnRight, this.onModelTurnRightHandler);
            Dispatcher.removeEventListener(EventName.FashionModelStopTurning, this.onModelStopTurningHandler);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            Global.stage.removeEventListener(MouseEvent.MOUSE_DOWN, this.onStageMouseDown);
            Global.stage.removeEventListener(MouseEvent.MOUSE_UP, this.onStageMouseUp);
            this._stageMouseDown = false;
            this._model = null;
            this._startX = 0;
            this._endX = 0;
            this._frameTimer = null;
            this._turnValue = 0;
            if (this._isAddListeners)
            {
                this.removeListeners();
            }
            return;
        }// end function

    }
}
